package cn.zjnu.study.controller;

import cn.zjnu.study.bean.Msg;
import cn.zjnu.study.bean.User;
import cn.zjnu.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    private boolean checkUserName(String userName) {
        if (userName.length() < 3 || userName.length() >= 18) {
            return false;
        }
        for (int i = 0; i < userName.length(); i++) {
            if (!(Character.isAlphabetic(userName.charAt(i)) || Character.isDigit(userName.charAt(i)) || userName.charAt(i) == '_')) {
                return false;
            }
        }
        return true;
    }

    private boolean checkPassword(String password) {
        if (password.length() < 6 || password.length() >= 18) {
            return false;
        }
        for (int i = 0; i < password.length(); i++) {
            if (!(Character.isAlphabetic(password.charAt(i)) || Character.isDigit(password.charAt(i)) || password.charAt(i) == '_')) {
                return false;
            }
        }
        return true;
    }

    @PostMapping("/user")
    public Msg register(@RequestParam String userName,
                        @RequestParam String password,
                        HttpSession session) {
        User user = userService.getUserByName(userName);
        if (user != null) {
            return Msg.error("User already exists");
        }
        if (!checkUserName(userName)) {
            return Msg.error("Invalid Username");
        }

        if (!checkPassword(password)) {
            return Msg.error("Invalid Password");
        }

        user = new User(null, userName, password, 0, null);
        userService.addUser(user);
        user = userService.getUserByName(userName);
        session.setAttribute("user", user);
        return Msg.success();
    }

    @GetMapping("/user")
    public Msg login(@RequestParam String userName,
                     @RequestParam String password,
                     HttpSession session) {

        User user = userService.getUserByName(userName);
        if (user == null) {
            return Msg.msg(1, "This user does not exists");
        }
        if (!password.equals(user.getPassword())) {
            return Msg.msg(2, "Wrong password");
        }
        session.setAttribute("user", user);
        return Msg.success();
    }

    @PutMapping("/user/info")
    public Msg updateInfo(@RequestParam String userName,
                          @RequestParam Integer gender,
                          HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (!checkUserName(userName)) {
            return Msg.error("Invalid UserName");
        }
        if (!user.getUserName().equals(userName) && userService.getUserByName(userName) != null) {
            return Msg.error("UserName already exists");
        }
        user.setUserName(userName);
        user.setGender(gender);
        userService.updateUser(user);
        return Msg.success();
    }

    @PutMapping("/user/pwd")
    public Msg changePassword(@RequestParam String currentPassword,
                              @RequestParam String newPassword,
                              HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (!user.getPassword().equals(currentPassword)) {
            return Msg.error("wrong current password");
        }
        if (!checkPassword(newPassword)) {
            return Msg.error("Invalid new password");
        }
        user.setPassword(newPassword);
        userService.updateUser(user);
        session.setAttribute("user", null);
        return Msg.success();

    }

    @PostMapping("/uploadIcon")
    public Msg uploadIcon(@RequestParam MultipartFile imageFile,
                          HttpSession session) {
        User user = (User) session.getAttribute("user");
        try {
            userService.uploadIcon(imageFile, user);
        } catch (IOException e) {
            return Msg.error();
        }
        return Msg.success();
    }

}
